package com.thinkgem.jeesite.modules.cloudfactory.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.support.json.JSONUtils;
import com.thinkgem.jeesite.common.utils.MD5EncryptionUtil;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryUser;
import com.thinkgem.jeesite.modules.cloudfactory.service.CloudfactoryUserService;

/**
 * 
* @ClassName: LoginCenterController
* @Description: 用户登陆注册、个人中心
* @author Stone
* @date 2018年9月2日
*
 */
@RequiresPermissions("user")
@Controller
@RequestMapping("${adminPath}/loginCenter")
public class LoginCenterController {
	
	@Autowired
	private CloudfactoryUserService cloudfactoryUserService;
	
	/**
	 * 
	* @Title: goLogin
	* @Description: 跳转到登陆
	* @param @param model
	* @param @param request
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/goLogin", method=RequestMethod.GET)
	public String goLogin(Model model,HttpServletRequest request){
		model.addAttribute("isLogin",0);
		return "cloudfactory/login";
	}
	
	/**
	 * 
	* @Title: gotoRegister
	* @Description: 跳转到注册
	* @param @param model
	* @param @param request
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/goRegister", method=RequestMethod.GET)
	public String goRegister(Model model,HttpServletRequest request){
		model.addAttribute("isLogin",0);
		return "cloudfactory/register";
	}
	
	
	/**
	 * 
	* @Title: saveRegister
	* @Description: 保存注册信息
	* @param @param request
	* @param @param response
	* @param @param model
	* @param @param redirectAttributes
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "/saveRegister", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public String saveRegister(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		
			String success = cloudfactoryUserService.saveRegister(request,model);

		return success;
	}
	
	/**
	 * 
	* @Title: toHome
	* @Description: 注册后跳转到首页
	* @param @param model
	* @param @param request
	* @param @param id
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/goIndex", method=RequestMethod.GET)
	public String toHome(Model model,HttpServletRequest request,String id){
		CloudfactoryUser user = cloudfactoryUserService.get(id);
		request.getSession().setAttribute("cloudfactoryUser",user );
		model.addAttribute("cloudfactoryUser",user);
		model.addAttribute("isLogin",1);
		return "cloudfactory/welcome";
	}
	
	/**
	 * 
	* @Title: toHome
	* @Description: 退出登录
	* @param @param model
	* @param @param request
	* @param @param id
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/goOut", method=RequestMethod.GET)
	public String toHome(Model model,HttpServletRequest request){
		request.getSession().removeAttribute("cloudfactoryUser");
		model.addAttribute("isLogin",0);
		return "cloudfactory/welcome";
	}
	/**
	 * 
	* @Title: toHome
	* @Description: 登录
	* @param @param model
	* @param @param request
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@ResponseBody
	@RequestMapping(value="/login", method=RequestMethod.GET)
	public String login(Model model,HttpServletRequest request){
		
		
		String name = request.getParameter("name");
		String psw = request.getParameter("psw");
		CloudfactoryUser cloudfactoryUser = new CloudfactoryUser();
		cloudfactoryUser.setLoginName(name);
		cloudfactoryUser.setLoginPassword(MD5EncryptionUtil.getEncryption(psw));
		Map<String,Object> map = new HashMap<String,Object>();
		
		List<CloudfactoryUser> users = cloudfactoryUserService.findList(cloudfactoryUser);
		if(users.size()>0) {
			map.put("success", "success");
			map.put("id", users.get(0).getId());
			model.addAttribute("isLogin",1);
		} else {
			map.put("success", "账号或密码错误，请重试");
		}
		
		return JSONUtils.toJSONString(map);
	}
}
